open(BUFF,"clinical_data.txt") or die "no file found\n";
print "Subject\tTIME\tEVENT\tAGE\n";
#$line=<BUFF>;
#$line=<BUFF>;
#$line=<BUFF>;
#$line=<BUFF>;
while($line=<BUFF>)
{
	chomp($line);
	@a = split("\t",$line);
	@last = split(/\/|-/,$a[1]);
	@death = split(/\/|-/,$a[2]);
	@initial = split(/\/|-/,$a[3]);
	@birth=split(/\/|-/,$a[4]);
	$last =0;
	$death =0;
	$initial =0;
	$birth = 0;
	if($last[1] ne "" && $last[1] ne "0")
	{
		$last = $last[2]*12*30+$last[1]*30+$last[0];
	}
	if($death[1] ne "" && $death[1] ne "0")
        {
                $death = $death[2]*12*30+$death[1]*30+$death[0];
        }
	if($initial[1] ne "" && $initial[1] ne "0")
        {
                $initial = $initial[2]*12*30+$initial[1]*30+$initial[0];
        }
	if($birth[1] ne "" && $birth[1] ne "0")
        {
                $birth = $birth[2]*12*30+$birth[1]*30+$birth[0];
        }
	#print "$last\t$death\t$initial\n";
	if($death != 0 && $birth != 0)
	{
		$survival = ($death-$initial)/30; 
		$age = ($death-$birth)/365;
		$survival = sprintf("%.0f",$survival);
		$age = sprintf("%.0f",$age);
		print "$a[0]\t$survival\t1\t$age\n";
		
	}
	elsif($last != 0 && $birth != 0)
        {
		$survival = ($last-$initial)/30;
		$age = ($last-$birth)/365;
		$survival = sprintf("%.0f",$survival);
		$age = sprintf("%.0f",$age);
		print "$a[0]\t$survival\t0\t$age\n";
	}
	else
	{

	}
	#print "$line\n@last\n";	
}
